package cy.dao.impl;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import cy.dao.BaseDaoI;

@Repository("baseDao")
public class BaseDaoImpl<T> implements BaseDaoI<T> {

	private SessionFactory sessionFactory;

	public Session cuSession() {
		return this.sessionFactory.getCurrentSession();
	}

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	@Autowired
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	@Override
	public Serializable save(T o) {
		return this.cuSession().save(o);
	}

	@Override
	public T get(String hql, Object[] params) {
		Query query = this.cuSession().createQuery(hql);
		if (params != null && params.length != 0) {
			for (int i = 0; i < params.length; i++) {
				query.setParameter(i, params[i]);
			}
			List<T> list = query.list();
			if (list != null && list.size() > 0) {
				return list.get(0);
			}
		}
		return null;
	}

	@Override
	public T get(String hql, Map<String, Object> params) {
		Query query = this.cuSession().createQuery(hql);
		if (params != null && !params.isEmpty()) {
			for (String key : params.keySet()) {
				query.setParameter(key, params.get(key));
			}
			List<T> list = query.list();
			if (list != null && list.size() > 0) {
				return list.get(0);
			}
		}

		return null;
	}

	@Override
	public void delete(T o) {

		this.cuSession().delete(o);
	}

	@Override
	public void update(T o) {

		this.cuSession().update(o);
	}

	@Override
	public void saveOrUpdate(T o) {

		this.cuSession().saveOrUpdate(o);
	}

	@Override
	public List<T> find(String hql) {
		Query query = this.cuSession().createQuery(hql);
		return query.list();
	}

	@Override
	public List<T> find(String hql, Map<String, Object> params) {
		Query query = this.cuSession().createQuery(hql);
		if (params != null && !params.isEmpty()) {
			for (String key : params.keySet()) {
				query.setParameter(key, params.get(key));
			}
		}
		return query.list();
	}

	@Override
	public List<T> find(String hql, int page, int rows) {
		Query query = this.cuSession().createQuery(hql);
		return query.setFirstResult((page - 1) * rows).setMaxResults(rows).list();

	}

	@Override
	public List<T> find(String hql, Map<String, Object> params, int page, int rows) {
		Query query = this.cuSession().createQuery(hql);
		if (params != null && !params.isEmpty()) {
			for (String key : params.keySet()) {
				query.setParameter(key, params.get(key));
			}
		}
		return query.setFirstResult((page - 1) * rows).setMaxResults(rows).list();
	}

	@Override
	public Long count(String hql) {
		Query query = this.cuSession().createQuery(hql);
		return (Long) query.uniqueResult();
	}

	@Override
	public Long count(String hql, Map<String, Object> params) {
		Query query = this.cuSession().createQuery(hql);
		if (params != null && !params.isEmpty()) {
			for (String key : params.keySet()) {
				query.setParameter(key, params.get(key));
			}
		}
		return (Long) query.uniqueResult();
	}

	@Override
	public T get(String hql) {
		Query query=this.cuSession().createQuery(hql);
		List<T> l = query.list();
		if (l != null && l.size() > 0) {
			return l.get(0);
		}
		return null;
	}

	@Override
	public int excuteHql(String hql) {
		Query query=this.cuSession().createQuery(hql);
		return query.executeUpdate();
	}

	@Override
	public T get(Class<T> c, String id) {
		return (T) this.cuSession().get(c, id);
	}
}
